package drive.Service.ServiceFormat;

import com.google.gson.Gson;
import drive.Mapper.*;
import drive.Model.*;
import drive.Service.ServiceInterface.CommentService;
import drive.pojo.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceA implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Value("${official.account.id}")
    private int OFFICIAL_ACCOUNT_ID; // 官方账号ID，从配置文件中获取

    @Autowired
    private ChatMessageMapper chatMessageMapper;

    @Autowired
    private ChatSessionMapper chatSessionMapper;
    private Gson gson = new Gson();

    @Override
    public String findAll() {
        List<Comment> list = commentMapper.findAll();
        return gson.toJson(list);
    }

    @Override
    public String findByCommentID(int commentID) {
        Comment comment = commentMapper.findByCommentID(commentID);
        return gson.toJson(comment);
    }

    @Override
    public String findByArticleID(int articleID) {
        List<Comment> list = commentMapper.findByArticleID(articleID);
        return gson.toJson(list);
    }

    @Override
    public String findByUserID(int userID) {
        List<Comment> list = commentMapper.findByUserID(userID);
        return gson.toJson(list);
    }

    @Override
    public String findByParentCommentID(int parentCommentID) {
        List<Comment> list = commentMapper.findByParentCommentID(parentCommentID);
        return gson.toJson(list);
    }

    @Override
    public String add(Comment com) {
//        if(comment.getParentCommentID()==0){
//            commentMapper.add_top(comment);
//            return "发布评论成功";
//        }
//        int result = commentMapper.add(comment);
//        return "发布评论成功";
        // 实现文章评论逻辑
        int articleID = com.getArticleID();
        int userID = com.getUserID();
        com.setCreationTime(new Date());
        if(com.getParentCommentID()==0){
            commentMapper.add_top(com);
            Article article = articleMapper.findById(articleID);
            User user = userMapper.selectUserById(userID);
            sendOfficialMessage(article.getAuthorID(),user.getUsername() +",评论了我的文章,"+com.getContent());
        }else{
            commentMapper.add(com);
            Comment c = commentMapper.findByCommentID(com.getParentCommentID());
            User user = userMapper.selectUserById(userID);
            sendOfficialMessage(c.getUserID(),user.getUsername() +",回复了你的评论:"+com.getContent());
        }
        articleMapper.updateArticleCommentCountByArticleID(articleID);
        return "游记评论成功";
    }

    private void sendOfficialMessage(int authorId, String messageContent) {
        ChatSession chatSession = chatSessionMapper.findByUserIDs(OFFICIAL_ACCOUNT_ID,authorId);
        if (chatSession == null) {
            // 创建会话
            chatSession = new ChatSession();
            chatSession.setUser1ID(OFFICIAL_ACCOUNT_ID);
            chatSession.setUser2ID(authorId);
            chatSession.setCreationTime(new Date());
            chatSessionMapper.add(chatSession);
            System.out.println(chatSession);
        }
        System.out.println("发送关注消息中");
        // 发送消息
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setChatSessionID(chatSession.getChatSessionID());
        chatMessage.setSenderID(OFFICIAL_ACCOUNT_ID);
        chatMessage.setContent(messageContent);
        chatMessage.setSendTime(new Date());
        chatMessage.setStatus(ChatMessage.Status.sending);
        chatMessageMapper.add(chatMessage);

        chatSessionMapper.update_time(chatSession.getChatSessionID());
    }

    @Override
    public String update(Comment comment) {
        int result = commentMapper.update(comment);
        return "修改评论成功";
    }

    @Override
    public String delete(int commentID) {
        int result = commentMapper.delete(commentID);
        return "删除评论成功";
    }
}